﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using WindowsForms.Class;
using WindowsForms.service;

namespace WindowsForms
{
    public partial class RegionManagement : DevExpress.XtraEditors.XtraForm
    {
        Service1 iQuery;
        List<Regions> lsRegion = null;
        public RegionManagement()
        {
            iQuery = new Service1();
            InitializeComponent();
            GetAllRegion();
        }

        private void btnAddRegion_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (RegionProcess frmRegion = new RegionProcess())
            {
                if (frmRegion.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    Regions regionObj = new Regions();
                    regionObj.RegionName = frmRegion.Region_Name;
                    regionObj.Description = frmRegion.Region_Des;
                    iQuery.InsertRegion(regionObj.RegionName, regionObj.Description);
                    lsRegion.Add(regionObj);
                    gvRegion.RefreshData();
                }
            }
        }

        private void GetAllRegion()
        {
            ConvertToList convert = new ConvertToList();
            lsRegion = convert.ConvertTolistRegions(iQuery.SelectRegionsAll().Tables[0]);
            gridControlRegion.DataSource = lsRegion;
            btnEditRegion.Enabled = false;
            btnDelRegion.Enabled = false;
        }

        private void btnEditRegion_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvRegion.GetSelectedRows();
            Regions regionSelectedt = gvRegion.GetRow(rows[rows.Length - 1]) as Regions;
           
            using (RegionProcess frmRegion = new RegionProcess(true))
            {
                //Update region
                frmRegion.Region_Name = regionSelectedt.RegionName;
                frmRegion.Region_Des = regionSelectedt.Description;

                if (frmRegion.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    regionSelectedt.RegionName = frmRegion.Region_Name;
                    regionSelectedt.Description = frmRegion.Region_Des;
                    iQuery.UpdateRegion(regionSelectedt.RegionID, regionSelectedt.RegionName, regionSelectedt.Description);
                    gvRegion.RefreshData();
                }
            }
        }

        private void gvRegion_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            btnEditRegion.Enabled = gvRegion.SelectedRowsCount > 0;
            btnDelRegion.Enabled = gvRegion.SelectedRowsCount > 0;
        }

        private void btnDelRegion_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvRegion.GetSelectedRows();
            Regions regionSelected = gvRegion.GetRow(rows[rows.Length - 1]) as Regions;
            string message = string.Format(Constants.MESS_CONFIRMDELETE, regionSelected.RegionName);
            DialogResult dr = MessageBox.Show(message, Constants.MESS_DELETE_CAPTION, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                iQuery.DeleteRegion(regionSelected.RegionID);
                lsRegion.Remove(regionSelected);
                gvRegion.RefreshData();
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GetAllRegion();
        }

        private void RegionManagement_Load(object sender, EventArgs e)
        {

        }
    }
}